// ScrollCommand.java - 鼠标滚动指令
package org.zjx.command.basic;

import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import lombok.extern.slf4j.Slf4j;
import org.zjx.command.Command;
import org.zjx.core.IWindow;
import org.zjx.core.impl.StartWindow;
import org.zjx.state.StateManager;

import java.awt.*;

@Slf4j
public class ScrollCommand extends Command {
    private final int amount;

    // 正数表示向上滚动
    public ScrollCommand(IWindow window, int amount, StateManager stateManager) {
        super(window, false, stateManager);
        this.amount = amount;
    }

    @Override
    protected void doExecute() throws Exception {
        // 计算滚轮偏移量（必须是WHEEL_DELTA的倍数）
        int wheelMovement = amount * 120;
        // 构造wParam：高16位=滚轮偏移量，低16位=虚拟按键状态（0表示无按键）
        long wParamValue = ((long) wheelMovement << 16) & 0xFFFF0000L;
        WinDef.WPARAM wParam = new WinDef.WPARAM(wParamValue);

        WinDef.LPARAM lParam = new WinDef.LPARAM(0);  // We don't need to move the cursor, so we use 0

        // Send the message to simulate the mouse wheel scrolling
        User32.INSTANCE.SendMessage(targetWindow.getHwnd(), 0x020A, wParam, lParam);
    }
}